﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;


[Serializable()]
public class TrueFalseQuestion : Question, ISerializable
{
    private bool _correctanswer;

    public TrueFalseQuestion(int id, string text, bool answer)
    {
        this._id = id;
        this._text = text;
        this._correctanswer = answer;
    }

    public override string getQuestionHtml()
    {
        StringBuilder sb = new StringBuilder();

        sb.Append("<div class=\"question\">");
        sb.Append("<span class=\"text\">" + _text + "</span>");
        sb.Append("</div>");

        return sb.ToString();
    }

    public override string getQuestionWithAnswerHtml()
    {
        StringBuilder sb = new StringBuilder();

        sb.Append("<div class=\"question_true_false\">");
        sb.Append("<span class=\"text\">" + _text + "</span>");
        sb.Append("<span class=\"answer\">");
        if (_correctanswer)
        {
            sb.Append("True");
        }
        else
        {
            sb.Append("False");
        }
        sb.Append("</span>");
        sb.Append("</div>");

        return sb.ToString();
    }

    public override string getQuestionXml()
    {
        StringBuilder sb = new StringBuilder();

        sb.Append("<TrueFalseQuestion>");
        sb.Append("<text>" + _text + "</text>");
        sb.Append("</TrueFalseQuestion>");

        return sb.ToString();
    }

    public override string getQuestionWithAnswerXml()
    {
        StringBuilder sb = new StringBuilder();

        sb.Append("<TrueFalseQuestion>");
        sb.Append("<text>" + _text + "</text>");
        sb.Append("<answer>");
        if (_correctanswer)
        {
            sb.Append("True");
        }
        else
        {
            sb.Append("False");
        }
        sb.Append("</answer>");
        sb.Append("</TrueFalseQuestion>");

        return sb.ToString();
    }

    public bool Correctanswer
    {
        get { return _correctanswer; }
        set { _correctanswer = value; }
    }

    public override string getCorrectAnswer()
    {
        if (_correctanswer)
            return "T";
        else
            return "F";
    }

    /// <summary>
    /// Deserialization constructor
    /// </summary>
    /// <param name="info"></param>
    /// <param name="ctxt"></param>
    public TrueFalseQuestion(SerializationInfo info, StreamingContext ctxt)
    {
        _id = (int)info.GetValue("_id", typeof(int));
        _text = (String)info.GetValue("_text", typeof(String));
        _correctanswer = (Boolean)info.GetValue("_correctanswer", typeof(Boolean));
    }

    /// <summary>
    /// Serialization function
    /// </summary>
    /// <param name="info"></param>
    /// <param name="ctxt"></param>
    public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    {
        info.AddValue("_id", _id);
        info.AddValue("_text", _text);
        info.AddValue("_correctanswer", _correctanswer);
    }
}
